package com.example.mobile2021606177cke0511;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private ImageView imageView;
    private TextView tvMainContent;
    final String IMAGEPATH="https://weibo.com/5044281310/KeXnkCvDJ?ref=feedsdk&type=comment#_rnd1620735922028";
    final String TEXTPATH="https://baike.baidu.com/item/%E6%9D%8E%E6%98%93%E5%B3%B0/3908480?fr=aladdin";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        new Thread() {
            public void run() {
                super.run();
                try {
                    URL imageUrl = new URL(IMAGEPATH);
                    HttpURLConnection imageConnection = (HttpURLConnection) imageUrl.openConnection();
                    imageConnection.setRequestMethod("GET");
                    imageConnection.setConnectTimeout(5000);
                    InputStream imageIS = imageConnection.getInputStream();
                    final Bitmap bitmap = BitmapFactory.decodeStream(imageIS);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setImageBitmap(bitmap);
                        }
                    });
                    imageIS.close();
                    imageConnection.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        new Thread() {
            public void run(){
                super.run();
        }
    }.start();
        try {
            URL textUrl=new URL(TEXTPATH);
            HttpURLConnection textConnection=(HttpURLConnection)textUrl.openConnection();
            InputStream textIs=textConnection.getInputStream();
            InputStreamReader isr=new InputStreamReader(textIs);
            final BufferedReader br=new BufferedReader(isr);
            String line=null;
            String temp="";
            while ((line=br.readLine())!=null){
                temp+=line;
                temp+="/n";
            }
            final String msg=temp;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvMainContent.setText(msg);
                }
            });
            br.close();
            isr.close();
            textIs.close();
            textConnection.disconnect();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
        imageView = (ImageView) findViewById(R.id.imageView);
        tvMainContent = (TextView) findViewById(R.id.tvMainContent);
    }
}